﻿// -----------------------------------------------------------------------
// <copyright file="CauHinhDataAccess.cs" company="">
// TODO: Copyright by dangppsoft@gmail.com.
// </copyright>
// -----------------------------------------------------------------------
namespace ManagerClinic.Core.DataAccess
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using ManagerClinic.Core.Common;
    using System.Data.Common;
    using System.Data;
    using Microsoft.Practices.EnterpriseLibrary.Data;
    using Microsoft.Practices.EnterpriseLibrary.Common;
    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
    using ManagerClinic.Ultilities.Ultility;

    public class CauHinhDataAccess
    {
        public Int64 InsertCauHinh(CauHinhEntity obj)
        {
            try
            {
                string sql = "spCauHinh_Insert";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddOutParameter(cmd, "MaCauHinh", DbType.Int64, int.MaxValue);
                db.AddInParameter(cmd, "MaNguoiDung", DbType.String, obj.MaNguoiDung);
                db.AddInParameter(cmd, "MaKhoa", DbType.String, obj.MaKhoa);
                db.ExecuteNonQuery(cmd);
                return (Int64)db.GetParameterValue(cmd, "MaCauHinh");
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("InsertCauHinh", ex.Message); throw ex; }
        }

        public bool EditCauHinh(CauHinhEntity obj)
        {
            try
            {
                string sql = "spCauHinh_Update";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaCauHinh", DbType.Int64, obj.MaCauHinh);
                db.AddInParameter(cmd, "MaNguoiDung", DbType.String, obj.MaNguoiDung);
                db.AddInParameter(cmd, "MaKhoa", DbType.String, obj.MaKhoa);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("UpdateCauHinh", ex.Message); throw ex; }
        }

        public bool DeleteCauHinh(Int64 maCauHinh)
        {
            try
            {
                string sql = "spCauHinh_DeleteByID";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaCauHinh", DbType.Int64, maCauHinh);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("DeleteCauHinh", ex.Message); throw ex; }
        }

        public CauHinhEntity GetCauHinh(Int64 maCauHinh)
        {
            try
            {
                CauHinhEntity obj = null;
                string sql = "spCauHinh_SelectByID";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaCauHinh", DbType.Int64, maCauHinh);
                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        obj = new CauHinhEntity();
                        obj.MaCauHinh = String.IsNullOrEmpty(reader["MaCauHinh"].ToString()) == true ? 0 : (Int64)reader["MaCauHinh"];
                        obj.MaNguoiDung = reader["MaNguoiDung"].ToString();
                        obj.MaKhoa = reader["MaKhoa"].ToString();
                    }
                }
                return obj;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("GetCauHinh", ex.Message); throw ex; }
        }

        public List<CauHinhEntity> GetCauHinhList()
        {
            try
            {
                List<CauHinhEntity> list = new List<CauHinhEntity>();
                CauHinhEntity obj = null;
                string sql = "spCauHinh_SelectAll";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        obj = new CauHinhEntity();
                        obj.MaCauHinh = String.IsNullOrEmpty(reader["MaCauHinh"].ToString()) == true ? 0 : (Int64)reader["MaCauHinh"];
                        obj.MaNguoiDung = reader["MaNguoiDung"].ToString();
                        obj.MaKhoa = reader["MaKhoa"].ToString();
                        obj.TenNguoiDung = reader["TenNguoiDung"].ToString();
                        list.Add(obj);
                    }
                }
                return list;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("GetCauHinhList", ex.Message); throw ex; }
        }

    }
}